/**
 * FriendData Class
 * desc : 
 */
function FriendData (mode){
	
	this.mode = mode;
	
	this.data = null;
	
	//return column number or null
//	this.colNum = findColNum(flag);
	
	//build up url
	this.url = app_dir+'/ajax/getFriendsDataAJAX.php?mode='+this.mode;
	
	//error
	this.error = null;

	//ajax request
	this.requestData = function (callbackFn){
	    if (this.data == null) {
	    	//using closure to create a new scope for send caller reference into callback function
	        $.getJSON(this.url, null, (function(caller){
	        	return function(response){
	            	caller.data = response;
	            	if(callbackFn != null && callbackFn != undefined){
			        	callbackFn.call();
		            }
	        	};
	        	})(this) //using closure to create new variable scope which includes caller reference
	        );
	    } else {
	    	if(callbackFn != null && callbackFn != undefined){
	    		callbackFn.call();
	    	}
	    }
	};
	
	this.addToGraph = function(){
		try{
			if (this.data != null) {
				var type = getDataTypeByFlag(flag);
				
				//add data into applet one by one
				$(this.data).each(function(index, item){
					var count = item.count - 0;
					graph.setScore(item.fId, type, count);
				});
			}
		}catch(err){
			trace2(err);
		}
	};
	
	this.addToTable = function(){
		//if data is not null
		if (this.data != null) {
			
			var colNum = this.colNum;
			
			//Get Weight of the data type
			var type = getDataTypeByFlag(flag);
			var weight = $("#"+type+"_weight_slider").slider("option", "value");
			
			//Add to the table 
			$(this.data).each(function(index, item){
				var obj = $('#row_' + item.fId + ' td:nth-child(' + colNum + ')');

				var score = (obj.text() - 0) + (item.count - 0)*weight;
				obj.text(score);
				
			});
			
			//Total
			this.calculateTotal();

		}
	};
	
	this.calculateTotal = function(){
	    //find object
		$(".rankTable tbody tr").each(function(index, row){
			var cols = $(row).children();
			
		   //find total column
		   var total = 0;
		   
		   //calculate total
		   for(var i = 2; i < 6; i++){
		   	total += cols.eq(i).text() - 0;
		   }
		   
		   //show total
		   cols.eq(6).text(total);
		   	
		});
	};
}

